﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;
using System.Collections.Specialized;


namespace sales.Controls
{
    //Incomplete
    public class gridVoucherDetail : common.controls.baseDataGridView 
    {
        public gridVoucherDetail()
        {
            try
            {
                this.myVoucherListSource.DataSource = this.myVoucherTbl;
                this.AutoArrange();
                this.myOnFind += new OnCellEvent(FindData);
                this.myAmountPrecision = common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskLocalAmt);
            }
            catch (Exception er)
            {
                this.ErrorHandler(this, er);
            }
        }

        public enum ColumnNames
        {
            voucherNo, voucherDate, amount, fAmount, taxAmt, taxPerc,
            saleCode, custCode, custName, catCode1, notes, refVoucherNo1, refVoucher1Date, refVoucherNo2, printBtn 
        };
        int myAmountPrecision = 0;

        BindingSource myVoucherListSource = new BindingSource();
        protected data.applicationDS.VoucherDataTable myVoucherTbl = new data.applicationDS.VoucherDataTable();
        protected BindingSource myCatCode1Source = new BindingSource();
        protected data.masterDS.voucherDetailCatCodeDataTable myCatCode1Tbl = new data.masterDS.voucherDetailCatCodeDataTable();

        public data.applicationDS.VoucherRow FindVoucher(string voucherId)
        {
            return myVoucherTbl.FindByvoucherId(voucherId);
        }
        public void LoadVoucher(StringCollection voucherIdList)
        {
            DataAccess.DataLibs.LoadData(this.myVoucherTbl, voucherIdList);
        }
        public void LoadCatCode1(int flagMask)
        {
            myCatCode1Tbl.Clear();
            DataAccess.DataCache.LoadVoucherDetailCatCode();
            for (int idx = 0; idx < DataAccess.DataCache.myMasterDS.voucherDetailCatCode.Count; idx++)
            {
                if ((DataAccess.DataCache.myMasterDS.voucherDetailCatCode[idx].IsflagNull())) continue;
                if ((DataAccess.DataCache.myMasterDS.voucherDetailCatCode[idx].flag & flagMask) <= 0) continue;
                myCatCode1Tbl.ImportRow(DataAccess.DataCache.myMasterDS.voucherDetailCatCode[idx]);
            }
            return;
        }
        public void LoadCatCode1()
        {
            myCatCode1Tbl.Clear();
            DataAccess.DataCache.LoadVoucherDetailCatCode();
            for (int idx = 0; idx < DataAccess.DataCache.myMasterDS.voucherDetailCatCode.Count; idx++)
            {
                myCatCode1Tbl.ImportRow(DataAccess.DataCache.myMasterDS.voucherDetailCatCode[idx]);
            }
            return;
        }

        public data.masterDS.voucherDetailCatCodeRow FindCatCode1(string code)
        {
            data.masterDS.voucherDetailCatCodeDataTable tbl = (data.masterDS.voucherDetailCatCodeDataTable)myCatCode1Source.DataSource;
            return tbl.FindBycode(code);
        }

        public override void Init(BindingSource dataSource)
        {
            base.Init(dataSource);
            data.applicationDS.VoucherDetailDataTable detailTbl = (data.applicationDS.VoucherDetailDataTable)dataSource.DataSource;

            DataGridViewCellStyle percentCellStyle = new DataGridViewCellStyle();
            DataGridViewCellStyle amountCellStyle = new DataGridViewCellStyle();
            DataGridViewCellStyle fAmountCellStyle = new DataGridViewCellStyle();
            DataGridViewCellStyle dateCellStyle = new DataGridViewCellStyle();

            percentCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            percentCellStyle.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskPercentTax);
            percentCellStyle.NullValue = null;

            amountCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            amountCellStyle.Format = "N" + this.myAmountPrecision;
            amountCellStyle.NullValue = null;

            fAmountCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            fAmountCellStyle.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskForeignAmt);
            fAmountCellStyle.NullValue = null;

            dateCellStyle.Format = "d"; dateCellStyle.NullValue = null;

            // 
            // voucherNo
            // 
            DataGridViewComboBoxColumn voucherNo = new DataGridViewComboBoxColumn();
            voucherNo.DataSource = this.myVoucherListSource;
            voucherNo.DataPropertyName = detailTbl.voucherIdColumn.ColumnName;
            voucherNo.DisplayMember = myVoucherTbl.voucherNoColumn.ColumnName;
            voucherNo.ValueMember = myVoucherTbl.voucherIdColumn.ColumnName;
            voucherNo.HeaderText = "Biên bản";
            voucherNo.Name = ColumnNames.voucherNo .ToString();
            voucherNo.ReadOnly = true;
            voucherNo.Width = 120;
            voucherNo.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            voucherNo.ReadOnly = true;
            voucherNo.Visible = false;

            // 
            // voucherDate
            // 
            DataGridViewComboBoxColumn voucherDate = new DataGridViewComboBoxColumn();
            voucherDate.DataSource = this.myVoucherListSource;
            voucherDate.DataPropertyName = detailTbl.voucherIdColumn.ColumnName;
            voucherDate.DisplayMember = myVoucherTbl.onDateColumn.ColumnName;
            voucherDate.ValueMember = myVoucherTbl.voucherIdColumn.ColumnName;
            voucherDate.HeaderText = "Ngày";
            voucherDate.Name = ColumnNames.voucherDate.ToString();
            voucherDate.ReadOnly = true;
            voucherDate.Width = 85;
            voucherDate.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            voucherDate.ReadOnly = true;
            voucherDate.Visible = false;
            voucherDate.DefaultCellStyle = dateCellStyle;

            // 
            // saleCode
            // 
            common.controls.DataGridViewTextBoxColumnExt saleCode = new common.controls.DataGridViewTextBoxColumnExt();
            saleCode.DataPropertyName = detailTbl.saleCodeColumn.ColumnName;
            saleCode.HeaderText = "Mã.bán.hàng";
            saleCode.Name = ColumnNames.saleCode.ToString();
            saleCode.Width = 130;
            saleCode.Visible = true;
            saleCode.Uppercase = true;

            // 
            // custCode
            // 
            common.controls.DataGridViewTextBoxColumnExt custCode = new common.controls.DataGridViewTextBoxColumnExt();
            custCode.DataPropertyName = detailTbl.custCodeColumn.ColumnName;
            custCode.HeaderText = "Mã KH";
            custCode.Name = ColumnNames.custCode.ToString();
            custCode.Width = 110;
            custCode.Visible = true;
            custCode.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            custCode.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;
            custCode.ReadOnly = true;
            custCode.Uppercase = true;

            // 
            // Cat code 1 
            // 
            BindingSource myCustomerSource = new BindingSource();
            myCustomerSource.DataSource = DataAccess.DataCache.myMasterDS.Customer;
            DataGridViewComboBoxColumn custNane = new DataGridViewComboBoxColumn();
            custNane.DataPropertyName = detailTbl.custCodeColumn.ColumnName;
            custNane.Name = ColumnNames.custName.ToString();
            custNane.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
            custNane.DataSource = myCustomerSource;
            custNane.DisplayMember = DataAccess.DataCache.myMasterDS.Customer.name1Column.ColumnName;
            custNane.ValueMember = DataAccess.DataCache.myMasterDS.Customer.custCodeColumn.ColumnName;
            custNane.HeaderText = "Tên khách hàng";
            custNane.Visible = false;
            custNane.Width = 200;
            custNane.ReadOnly = true;
            custNane.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

            // 
            // amount
            // 
            DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
            amount.DataPropertyName = detailTbl.amountColumn.ColumnName;
            amount.DefaultCellStyle = amountCellStyle;
            amount.HeaderText = "Số tiền";
            amount.Name = ColumnNames.amount.ToString();
            amount.Width = 110;
            amount.Visible = true;

            // 
            // fAmount
            // 
            DataGridViewTextBoxColumn fAmount = new DataGridViewTextBoxColumn();
            fAmount.DataPropertyName = detailTbl.fAmountColumn.ColumnName;
            fAmount.DefaultCellStyle = fAmountCellStyle;
            fAmount.HeaderText = "Nguyên tệ";
            fAmount.Name = ColumnNames.fAmount.ToString();
            fAmount.Width = 100;
            fAmount.Visible = false;

            // 
            // tax amount
            // 
            DataGridViewTextBoxColumn taxAmt = new DataGridViewTextBoxColumn();
            taxAmt.DataPropertyName = detailTbl.taxAmtColumn.ColumnName;
            taxAmt.DefaultCellStyle = amountCellStyle;
            taxAmt.HeaderText = "Thuế";
            taxAmt.Name = ColumnNames.taxAmt.ToString();
            taxAmt.Width = 90;
            taxAmt.Visible = false;

            // 
            // tax percent
            // 
            DataGridViewTextBoxColumn taxPerc = new DataGridViewTextBoxColumn();
            taxPerc.DataPropertyName = detailTbl.taxPercColumn.ColumnName;
            taxPerc.DefaultCellStyle = percentCellStyle;
            taxPerc.HeaderText = "%";
            taxPerc.Name = ColumnNames.taxPerc.ToString();
            taxPerc.Width = 40;
            taxPerc.Visible = false;

            // 
            // Cat code 1 
            // 
            myCatCode1Source.DataSource = myCatCode1Tbl;
            DataGridViewComboBoxColumn catCode1 = new DataGridViewComboBoxColumn();
            catCode1.DataPropertyName = detailTbl.catCode1Column.ColumnName;
            catCode1.Name = ColumnNames.catCode1.ToString();
            catCode1.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
            catCode1.DataSource = myCatCode1Source;
            catCode1.DisplayMember = myCatCode1Tbl.descriptionColumn.ColumnName;
            catCode1.ValueMember = myCatCode1Tbl.codeColumn.ColumnName;
            catCode1.HeaderText = "Phân.loại";
            catCode1.Visible = true;
            catCode1.Width = 100;

            // 
            // refVoucherNo1
            // 
            common.controls.DataGridViewTextBoxColumnExt refVoucherNo1 = new common.controls.DataGridViewTextBoxColumnExt();
            refVoucherNo1.DataPropertyName = detailTbl.refVoucherNo1Column.ColumnName;
            refVoucherNo1.Name = ColumnNames.refVoucherNo1.ToString();
            refVoucherNo1.HeaderText = "Số.chứng.từ";
            refVoucherNo1.Width = 110;
            refVoucherNo1.Uppercase = true;
            refVoucherNo1.Visible = false;

            // 
            // refVoucherDate
            // 
            DataGridViewTextBoxColumn refVoucherDate1 = new DataGridViewTextBoxColumn();
            refVoucherDate1.DataPropertyName = detailTbl.refVoucherDate1Column.ColumnName;
            refVoucherDate1.Name = ColumnNames.refVoucher1Date.ToString();
            refVoucherDate1.HeaderText = "Ngày";
            refVoucherDate1.Width = 85;
            refVoucherDate1.Visible = true;
            refVoucherDate1.DefaultCellStyle = dateCellStyle;

            // 
            // refVoucherNo2
            // 
            common.controls.DataGridViewTextBoxColumnExt refVoucherNo2 = new common.controls.DataGridViewTextBoxColumnExt();
            refVoucherNo2.DataPropertyName = detailTbl.refVoucherNo2Column.ColumnName;
            refVoucherNo2.Name = ColumnNames.refVoucherNo2.ToString();
            refVoucherNo2.HeaderText = "Số.chứng.từ";
            refVoucherNo2.Width = 110;
            refVoucherNo2.Uppercase = true;
            refVoucherNo2.Visible = true;
            // 
            // notes
            // 
            DataGridViewTextBoxColumn notes = new DataGridViewTextBoxColumn();
            notes.DataPropertyName = detailTbl.notesColumn.ColumnName;
            notes.Name = ColumnNames.notes.ToString();
            notes.Visible = true;
            notes.HeaderText = "Ghi chú";

            // 
            // actionBtn1
            // 
            common.controls.gridViewImageColumn printImgCol = new common.controls.gridViewImageColumn();
            printImgCol.myImageType = common.controls.imageType.Print;
            printImgCol.Name = sales.Controls.gridVoucherDetail.ColumnNames.printBtn.ToString();
            printImgCol.HeaderText = "";
            printImgCol.Width = 20;
            printImgCol.Visible = false;

            this.Columns.Clear();
            this.Columns.AddRange(new DataGridViewColumn[] {
                    voucherNo,voucherDate,saleCode,custCode,custNane, fAmount,amount,taxAmt,taxPerc,
                    refVoucherNo1,refVoucherDate1,refVoucherNo2,catCode1,notes,printImgCol});
            this.ScrollBars = ScrollBars.Vertical;

            this.DisbaleReadOnlyColumns();
            this.myAutoFitColumn = ColumnNames.notes.ToString();
        }

        public void DisableCATCODE()
        {
            DataGridViewComboBoxColumn catCode1 = (DataGridViewComboBoxColumn)this.Columns[ColumnNames.catCode1.ToString()] ;
            catCode1.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            catCode1.ReadOnly = true;
        }
        protected override void OnDefaultValuesNeeded(DataGridViewRowEventArgs e)
        {
            base.OnDefaultValuesNeeded(e);
            if (e == null) return;
            string SaveCatCode1 = "";
            if (this.CurrentRow != null)
                SaveCatCode1 = this.CurrentRow.Cells[ColumnNames.catCode1.ToString()].Value.ToString();
            if (myDataSource.Current != null)
                DataAccess.DataLibs.InitData((data.applicationDS.VoucherDetailRow)((DataRowView)myDataSource.Current).Row);
            if (this.Columns[ColumnNames.catCode1.ToString()].Visible && SaveCatCode1 != "")
                e.Row.Cells[ColumnNames.catCode1.ToString()].Value = SaveCatCode1;
        }
        protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
        {
            base.OnCellValueChanged(e);
            if (this.CurrentRow == null) return;

            if ( (this.Columns[e.ColumnIndex].Name == sales.Controls.gridVoucherDetail.ColumnNames.taxPerc.ToString() ||
                  this.Columns[e.ColumnIndex].Name == sales.Controls.gridVoucherDetail.ColumnNames.amount.ToString()) &&
                  this.Columns[sales.Controls.gridVoucherDetail.ColumnNames.taxAmt.ToString()].Visible &&
                  this.Columns[sales.Controls.gridVoucherDetail.ColumnNames.taxPerc.ToString()].Visible &&
                  this.Columns[sales.Controls.gridVoucherDetail.ColumnNames.amount.ToString()].Visible)
            {
                double amt = 0, taxPerc = 0;
                double.TryParse(this.CurrentRow.Cells[sales.Controls.gridVoucherDetail.ColumnNames.amount.ToString()].Value.ToString(), out amt);
                double.TryParse(this.CurrentRow.Cells[sales.Controls.gridVoucherDetail.ColumnNames.taxPerc.ToString()].Value.ToString(), out taxPerc);
                this.CurrentRow.Cells[sales.Controls.gridVoucherDetail.ColumnNames.taxAmt.ToString()].Value = Math.Round(amt * taxPerc / 100, this.myAmountPrecision);
            }

            if (this.Columns[e.ColumnIndex].Name == sales.Controls.gridVoucherDetail.ColumnNames.custCode.ToString() &&
                this.Columns[sales.Controls.gridVoucherDetail.ColumnNames.custName.ToString()].Visible)
            {
                DataAccess.DataCache.FindCUSTOMER(this.CurrentRow.Cells[sales.Controls.gridVoucherDetail.ColumnNames.custCode.ToString()].Value.ToString()); 
            }
        }

        protected override void OnCellValidated(DataGridViewCellEventArgs e)
        {
            base.OnCellValidated(e);
            if (this.CurrentRow == null) return;
            if (this.Columns[e.ColumnIndex].Name == sales.Controls.gridVoucherDetail.ColumnNames.saleCode.ToString() &&
                this.Columns[sales.Controls.gridVoucherDetail.ColumnNames.custCode.ToString()].Visible)
            {
                bool ro  = this.CurrentRow.Cells[sales.Controls.gridVoucherDetail.ColumnNames.saleCode.ToString()].Value.ToString().Trim()!="";
                this.SetColumnReadOnly(sales.Controls.gridVoucherDetail.ColumnNames.custCode.ToString(), ro);
                this.DisbaleReadOnlyColumns();
            }
        }

        private void FindData(object sender, common.controls.GridEventArgs e)
        {
            string findCode = this.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString(); ;
            if (this.CurrentCell == null) return;
            
            string columName = this.Columns[e.ColumnIndex].Name;
            if (columName == ColumnNames.custCode.ToString())
            {
                string code = this.CurrentCell.FormattedValue.ToString();
                if (code.Trim() == "") return;
                if (application.dataFind.myCustomerFind.Find(code))
                {
                    this.CurrentRow.Cells[columName].Value = application.dataFind.myCustomerFind.selectedDataRow.custCode;
                    this.myDataSource.ResetCurrentItem();
                    this.Focus();
                }
                else this.CurrentRow.Cells[columName].Value = null;
            }
            if (columName == ColumnNames.saleCode.ToString())
            {
                string code = this.CurrentCell.FormattedValue.ToString();
                if (code.Trim() == "") return;
                if (application.dataFind.mySaleCodeFind.Find(code))
                {
                    this.CurrentRow.Cells[columName].Value = application.dataFind.mySaleCodeFind.selectedDataRow.saleCode;
                    this.myDataSource.ResetCurrentItem();
                }
                else
                {
                    this.CurrentRow.Cells[columName].Value = null;
                    this.CurrentRow.Cells[ColumnNames.custCode.ToString()].Value = null;
                }
            }
        }
    }
}
